package com.zhiwei.codesecurity.web.api.taskmanage.controller;

import com.zhiwei.codesecurity.service.checkmarx.ICheckMarxService;
import com.zhiwei.codesecurity.web.api.taskmanage.request.CancelScanReq;
import com.zhiwei.codesecurity.web.common.base.BaseController;
import com.zhiwei.codesecurity.web.common.base.BaseResponse;
import com.zhiwei.codesecurity.web.common.base.RequestHead;
import com.zhiwei.codesecurity.web.common.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@Controller
@RequestMapping("/taskmanage")
public class TaskScanController extends BaseController {

    @Autowired
    @Qualifier("checkMarxService")
    private ICheckMarxService checkMarxService;

    /**
     * 取消扫描任务
     * @param head
     * @param cancelScanReq
     * @return
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public BaseResponse cancelScan(@RequestBody RequestHead head, @Valid @RequestBody CancelScanReq cancelScanReq,
                                   BindingResult result) {
        logger.info(">>> process: 取消扫描任务|interfaceId={},runId={}", cancelScanReq.getInterfaceId(),
                cancelScanReq.getRunId());
        if (ResponseUtil.existsParamsError(result)) {
            logger.error(">>> process: 取消扫描任务失败!");
            return ResponseUtil.getErrorParams(result);
        }

        checkMarxService.cancelScan(Long.valueOf(cancelScanReq.getInterfaceId()), cancelScanReq.getRunId());
        logger.info(">>> process: 取消扫描任务成功!");
        return ResponseUtil.getSuccessResponse();
    }
}
